package com.asa.control_theory.utils;

import com.asa.control_theory.utils.bean.ZhuangTai_Matrix;
import com.asa.hutils.MathHUtils;
import com.asa.hutils.Ni;
import com.asa.numanaly.F;

/**
 * 任意矩阵到矩阵的标准型的转换 
 * 	能控标准1型
 * 	能观标准2型
 * 	对角线标准形
 * 	Jordan 标准形
 * 
 */
public class GongShi2 {
	

	/**
	 * 任意矩阵转化成对角线矩阵
	 * @param abcd
	 * @return	不同系统选取得出的特征向量不同，肯定存在除了A其他数据结果都不同的情况
	 * @throws Exception
	 */
	public static ZhuangTai_Matrix duijiaoxian(ZhuangTai_Matrix abcd) throws Exception {
		
		double[][] a = abcd.getA();
		double[][] b = abcd.getB();
		double[][] c = abcd.getC();
		double[][] d = abcd.getD();
		double[][][] xax = F.XAX(a);
		
		double[][] xax_X = xax[0];
		double[][] xax_A = xax[1];
		System.out.println("特征向量：");
		MathHUtils.beauty(xax_X, 2);
		xax_A = MathHUtils.zhuanzhi(xax_X);
		
		
		MathHUtils.print(xax_A);
		System.out.println("=============");
		
//		double[][] ni2 = MathHUtils.juzhengni(xax_A);
//		MathHUtils.beauty(ni2, 2);
//		MathHUtils.print(ni2);

		
		double[][] P_NI = MathHUtils.juzhengni(xax_X);

//		a = MathHUtils.zhuanzhi(a);
//		double[][] chenfa = MathHUtils.chenfa(xax_X, a);
//		double[][] A = MathHUtils.chenfa(chenfa, P_NI);
		
		
		xax_X = MathHUtils.zhuanzhi(xax_X);
		P_NI = MathHUtils.zhuanzhi(P_NI);
		double[][] chenfa = MathHUtils.chenfa(P_NI, a);
		double[][] A = MathHUtils.chenfa(chenfa, xax_X);

		
		MathHUtils.beauty(A, 2);
		MathHUtils.print(A);

		
		double[] juzhengtezhengzhi = MathHUtils.juzhengtezhengzhi(A);
		
		for (int i = 0; i < juzhengtezhengzhi.length; i++) {
			System.out.println(juzhengtezhengzhi[i]);
		}
		
//		b = MathHUtils.zhuanzhi(b);

		double[][] B = MathHUtils.chenfa(P_NI, b);

		
		
		double[][] C = MathHUtils.chenfa(c, xax_X);

		MathHUtils.print(C);

		
		double[][] D = MathHUtils.copy(d);

		
		
		ZhuangTai_Matrix result = new ZhuangTai_Matrix();
		result.setA(A);
		result.setB(B);
		result.setC(C);
		result.setD(D);

		return result;

	}

	
	
	
	/**
	 * 能控1型转换成对角线形式
	 * 需要注意，必须是能控1型的
	 * （约旦标准型是对角线形式的一种，我是这么理解的，也可以这么的算）约旦并不是对角线
	 * @param abcd
	 * @return
	 * @throws Exception
	 */
	public static ZhuangTai_Matrix nengkong1_duijiaoxian(ZhuangTai_Matrix abcd) throws Exception {
		//实现思路就是，对角线这种的，对角线上的都是特征值
		//也就是说是特征值变换

		double[][] a = abcd.getA();
		double[][] b = abcd.getB();
		double[][] c = abcd.getC();
		double[][] d = abcd.getD();

		double[][] P = MatrixUtils.Vandemone(a);
		
		double[][] P_NI = MathHUtils.juzhengni(P);

		double[][] chenfa = MathHUtils.chenfa(P_NI, a);
		double[][] A = MathHUtils.chenfa(chenfa, P);
		
		MathHUtils.beauty(A, 2);
		
		double[][] B = MathHUtils.chenfa(P_NI,b);
		

		double[][] C = MathHUtils.chenfa(c, P);
		MathHUtils.print(C);

		
		double[][] D = MathHUtils.copy(d);

		ZhuangTai_Matrix result = new ZhuangTai_Matrix();
		result.setA(A);
		result.setB(B);
		result.setC(C);
		result.setD(D);

		return result;
		

	}

	
	
	public static void main(String[] args) throws Exception {
		
//		double[]  b= {0,1,3};
//		double[]  a= {1,3,2};
//		ZhuangTai_Matrix nengkong1 = GongShi.nengkong1(a, b);
		
		
		ZhuangTai_Matrix nengkong1 = new ZhuangTai_Matrix();
//		double[][] ma = {{0,1,0},
//				{0,0,1},
//				{-6,-11,-6}};
//		double[][] mb = {{0},
//				{0},
//				{6}};
//		double[][] mc = {{1,0,0}};
//		double[][] md = {{0}};
		
//		double[][] ma = {{0,1},
//				{-2,-3}};
//		double[][] mb = {{0},
//				{1}};
//		double[][] mc = {{6,0}};
//		double[][] md = {{0}};
		
		double[][] ma = {{-2,0},
		{0,-1}};
		double[][] mb = {{0},
				{1}};
		double[][] mc = {{6,0}};
		double[][] md = {{0}};
		
		
		
//		double[][] ma = {{2,-1,-1},
//		{0,-1,0},
//		{0,2,1}};
//		double[][] mb = {{7},
//				{2},
//				{3}};
//		double[][] mc = {{1,0,0}};
//		double[][] md = {{0}};
		
		
		
		
		double[] juzhengtezhengzhi = MathHUtils.juzhengtezhengzhi(ma);
		
		for (int i = 0; i < juzhengtezhengzhi.length; i++) {
			System.out.println(juzhengtezhengzhi[i]+"................................asa");
		}
		
		
		
//		double[][] ma = {{2,-1,-1},
//		{0,-1,0},
//		{0,2,1}};
//		double[][] mb = {{7},
//				{2},
//				{3}};
//		double[][] mc = {{1,0,0}};
//		double[][] md = {{0}};
		
		nengkong1.setA(ma);
		nengkong1.setB(mb);
		nengkong1.setC(mc);
		nengkong1.setD(md);

		
//		GongShi.print(nengkong1);
//		System.out.println("=============");
//		
//		ZhuangTai_Matrix duijiaoxian = nengkong1_duijiaoxian(nengkong1);
//		System.out.println("resultresultresultresultresult");
//
//		GongShi.print(duijiaoxian);

		System.out.println(",..................");
		ZhuangTai_Matrix duijiaoxian2 = duijiaoxian(nengkong1);
		GongShi.print(duijiaoxian2);
		
	}
	
	
	
	
	
	
	
}
